{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "57da752f",
   "metadata": {},
   "source": [
    "# 模型说明"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3244850f",
   "metadata": {},
   "source": [
    "- 可直接使用\n",
    "- 可以使用“多项式拟合”来提升模型效果"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60e62bba",
   "metadata": {},
   "source": [
    "# 使用SKlearn实现线性回归"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2ec4d0f",
   "metadata": {},
   "source": [
    "## 环境配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7cd179cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a18d9597",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif']=['Simhei']\n",
    "plt.rcParams['axes.unicode_minus']=False\n",
    "plt.style.use('ggplot')\n",
    "plt.rcParams['font.size'] = 12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ded78f1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression                       # 线性回归模型\n",
    "from sklearn.model_selection import train_test_split, cross_val_score   # 划分测试集、交叉验证\n",
    "from sklearn.preprocessing import StandardScaler                        # 数据预处理 - 归一化\n",
    "from sklearn.metrics import mean_squared_error                          # 均方误差\n",
    "from sklearn.preprocessing import PolynomialFeatures                    # 多项式拟合"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0215bfd7",
   "metadata": {},
   "source": [
    "## 数据导入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9bd613ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_boston #加利福尼亚房屋价值数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "50c59555",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\xue\\appdata\\local\\programs\\python\\python39\\lib\\site-packages\\sklearn\\utils\\deprecation.py:87: FutureWarning: Function load_boston is deprecated; `load_boston` is deprecated in 1.0 and will be removed in 1.2.\n",
      "\n",
      "    The Boston housing prices dataset has an ethical problem. You can refer to\n",
      "    the documentation of this function for further details.\n",
      "\n",
      "    The scikit-learn maintainers therefore strongly discourage the use of this\n",
      "    dataset unless the purpose of the code is to study and educate about\n",
      "    ethical issues in data science and machine learning.\n",
      "\n",
      "    In this special case, you can fetch the dataset from the original\n",
      "    source::\n",
      "\n",
      "        import pandas as pd\n",
      "        import numpy as np\n",
      "\n",
      "        data_url = \"http://lib.stat.cmu.edu/datasets/boston\"\n",
      "        raw_df = pd.read_csv(data_url, sep=\"\\s+\", skiprows=22, header=None)\n",
      "        data = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :2]])\n",
      "        target = raw_df.values[1::2, 2]\n",
      "\n",
      "    Alternative datasets include the California housing dataset (i.e.\n",
      "    :func:`~sklearn.datasets.fetch_california_housing`) and the Ames housing\n",
      "    dataset. You can load the datasets as follows::\n",
      "\n",
      "        from sklearn.datasets import fetch_california_housing\n",
      "        housing = fetch_california_housing()\n",
      "\n",
      "    for the California housing dataset and::\n",
      "\n",
      "        from sklearn.datasets import fetch_openml\n",
      "        housing = fetch_openml(name=\"house_prices\", as_frame=True)\n",
      "\n",
      "    for the Ames housing dataset.\n",
      "  warnings.warn(msg, category=FutureWarning)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'data': array([[6.3200e-03, 1.8000e+01, 2.3100e+00, ..., 1.5300e+01, 3.9690e+02,\n",
       "         4.9800e+00],\n",
       "        [2.7310e-02, 0.0000e+00, 7.0700e+00, ..., 1.7800e+01, 3.9690e+02,\n",
       "         9.1400e+00],\n",
       "        [2.7290e-02, 0.0000e+00, 7.0700e+00, ..., 1.7800e+01, 3.9283e+02,\n",
       "         4.0300e+00],\n",
       "        ...,\n",
       "        [6.0760e-02, 0.0000e+00, 1.1930e+01, ..., 2.1000e+01, 3.9690e+02,\n",
       "         5.6400e+00],\n",
       "        [1.0959e-01, 0.0000e+00, 1.1930e+01, ..., 2.1000e+01, 3.9345e+02,\n",
       "         6.4800e+00],\n",
       "        [4.7410e-02, 0.0000e+00, 1.1930e+01, ..., 2.1000e+01, 3.9690e+02,\n",
       "         7.8800e+00]]),\n",
       " 'target': array([24. , 21.6, 34.7, 33.4, 36.2, 28.7, 22.9, 27.1, 16.5, 18.9, 15. ,\n",
       "        18.9, 21.7, 20.4, 18.2, 19.9, 23.1, 17.5, 20.2, 18.2, 13.6, 19.6,\n",
       "        15.2, 14.5, 15.6, 13.9, 16.6, 14.8, 18.4, 21. , 12.7, 14.5, 13.2,\n",
       "        13.1, 13.5, 18.9, 20. , 21. , 24.7, 30.8, 34.9, 26.6, 25.3, 24.7,\n",
       "        21.2, 19.3, 20. , 16.6, 14.4, 19.4, 19.7, 20.5, 25. , 23.4, 18.9,\n",
       "        35.4, 24.7, 31.6, 23.3, 19.6, 18.7, 16. , 22.2, 25. , 33. , 23.5,\n",
       "        19.4, 22. , 17.4, 20.9, 24.2, 21.7, 22.8, 23.4, 24.1, 21.4, 20. ,\n",
       "        20.8, 21.2, 20.3, 28. , 23.9, 24.8, 22.9, 23.9, 26.6, 22.5, 22.2,\n",
       "        23.6, 28.7, 22.6, 22. , 22.9, 25. , 20.6, 28.4, 21.4, 38.7, 43.8,\n",
       "        33.2, 27.5, 26.5, 18.6, 19.3, 20.1, 19.5, 19.5, 20.4, 19.8, 19.4,\n",
       "        21.7, 22.8, 18.8, 18.7, 18.5, 18.3, 21.2, 19.2, 20.4, 19.3, 22. ,\n",
       "        20.3, 20.5, 17.3, 18.8, 21.4, 15.7, 16.2, 18. , 14.3, 19.2, 19.6,\n",
       "        23. , 18.4, 15.6, 18.1, 17.4, 17.1, 13.3, 17.8, 14. , 14.4, 13.4,\n",
       "        15.6, 11.8, 13.8, 15.6, 14.6, 17.8, 15.4, 21.5, 19.6, 15.3, 19.4,\n",
       "        17. , 15.6, 13.1, 41.3, 24.3, 23.3, 27. , 50. , 50. , 50. , 22.7,\n",
       "        25. , 50. , 23.8, 23.8, 22.3, 17.4, 19.1, 23.1, 23.6, 22.6, 29.4,\n",
       "        23.2, 24.6, 29.9, 37.2, 39.8, 36.2, 37.9, 32.5, 26.4, 29.6, 50. ,\n",
       "        32. , 29.8, 34.9, 37. , 30.5, 36.4, 31.1, 29.1, 50. , 33.3, 30.3,\n",
       "        34.6, 34.9, 32.9, 24.1, 42.3, 48.5, 50. , 22.6, 24.4, 22.5, 24.4,\n",
       "        20. , 21.7, 19.3, 22.4, 28.1, 23.7, 25. , 23.3, 28.7, 21.5, 23. ,\n",
       "        26.7, 21.7, 27.5, 30.1, 44.8, 50. , 37.6, 31.6, 46.7, 31.5, 24.3,\n",
       "        31.7, 41.7, 48.3, 29. , 24. , 25.1, 31.5, 23.7, 23.3, 22. , 20.1,\n",
       "        22.2, 23.7, 17.6, 18.5, 24.3, 20.5, 24.5, 26.2, 24.4, 24.8, 29.6,\n",
       "        42.8, 21.9, 20.9, 44. , 50. , 36. , 30.1, 33.8, 43.1, 48.8, 31. ,\n",
       "        36.5, 22.8, 30.7, 50. , 43.5, 20.7, 21.1, 25.2, 24.4, 35.2, 32.4,\n",
       "        32. , 33.2, 33.1, 29.1, 35.1, 45.4, 35.4, 46. , 50. , 32.2, 22. ,\n",
       "        20.1, 23.2, 22.3, 24.8, 28.5, 37.3, 27.9, 23.9, 21.7, 28.6, 27.1,\n",
       "        20.3, 22.5, 29. , 24.8, 22. , 26.4, 33.1, 36.1, 28.4, 33.4, 28.2,\n",
       "        22.8, 20.3, 16.1, 22.1, 19.4, 21.6, 23.8, 16.2, 17.8, 19.8, 23.1,\n",
       "        21. , 23.8, 23.1, 20.4, 18.5, 25. , 24.6, 23. , 22.2, 19.3, 22.6,\n",
       "        19.8, 17.1, 19.4, 22.2, 20.7, 21.1, 19.5, 18.5, 20.6, 19. , 18.7,\n",
       "        32.7, 16.5, 23.9, 31.2, 17.5, 17.2, 23.1, 24.5, 26.6, 22.9, 24.1,\n",
       "        18.6, 30.1, 18.2, 20.6, 17.8, 21.7, 22.7, 22.6, 25. , 19.9, 20.8,\n",
       "        16.8, 21.9, 27.5, 21.9, 23.1, 50. , 50. , 50. , 50. , 50. , 13.8,\n",
       "        13.8, 15. , 13.9, 13.3, 13.1, 10.2, 10.4, 10.9, 11.3, 12.3,  8.8,\n",
       "         7.2, 10.5,  7.4, 10.2, 11.5, 15.1, 23.2,  9.7, 13.8, 12.7, 13.1,\n",
       "        12.5,  8.5,  5. ,  6.3,  5.6,  7.2, 12.1,  8.3,  8.5,  5. , 11.9,\n",
       "        27.9, 17.2, 27.5, 15. , 17.2, 17.9, 16.3,  7. ,  7.2,  7.5, 10.4,\n",
       "         8.8,  8.4, 16.7, 14.2, 20.8, 13.4, 11.7,  8.3, 10.2, 10.9, 11. ,\n",
       "         9.5, 14.5, 14.1, 16.1, 14.3, 11.7, 13.4,  9.6,  8.7,  8.4, 12.8,\n",
       "        10.5, 17.1, 18.4, 15.4, 10.8, 11.8, 14.9, 12.6, 14.1, 13. , 13.4,\n",
       "        15.2, 16.1, 17.8, 14.9, 14.1, 12.7, 13.5, 14.9, 20. , 16.4, 17.7,\n",
       "        19.5, 20.2, 21.4, 19.9, 19. , 19.1, 19.1, 20.1, 19.9, 19.6, 23.2,\n",
       "        29.8, 13.8, 13.3, 16.7, 12. , 14.6, 21.4, 23. , 23.7, 25. , 21.8,\n",
       "        20.6, 21.2, 19.1, 20.6, 15.2,  7. ,  8.1, 13.6, 20.1, 21.8, 24.5,\n",
       "        23.1, 19.7, 18.3, 21.2, 17.5, 16.8, 22.4, 20.6, 23.9, 22. , 11.9]),\n",
       " 'feature_names': array(['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD',\n",
       "        'TAX', 'PTRATIO', 'B', 'LSTAT'], dtype='<U7'),\n",
       " 'DESCR': \".. _boston_dataset:\\n\\nBoston house prices dataset\\n---------------------------\\n\\n**Data Set Characteristics:**  \\n\\n    :Number of Instances: 506 \\n\\n    :Number of Attributes: 13 numeric/categorical predictive. Median Value (attribute 14) is usually the target.\\n\\n    :Attribute Information (in order):\\n        - CRIM     per capita crime rate by town\\n        - ZN       proportion of residential land zoned for lots over 25,000 sq.ft.\\n        - INDUS    proportion of non-retail business acres per town\\n        - CHAS     Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)\\n        - NOX      nitric oxides concentration (parts per 10 million)\\n        - RM       average number of rooms per dwelling\\n        - AGE      proportion of owner-occupied units built prior to 1940\\n        - DIS      weighted distances to five Boston employment centres\\n        - RAD      index of accessibility to radial highways\\n        - TAX      full-value property-tax rate per $10,000\\n        - PTRATIO  pupil-teacher ratio by town\\n        - B        1000(Bk - 0.63)^2 where Bk is the proportion of black people by town\\n        - LSTAT    % lower status of the population\\n        - MEDV     Median value of owner-occupied homes in $1000's\\n\\n    :Missing Attribute Values: None\\n\\n    :Creator: Harrison, D. and Rubinfeld, D.L.\\n\\nThis is a copy of UCI ML housing dataset.\\nhttps://archive.ics.uci.edu/ml/machine-learning-databases/housing/\\n\\n\\nThis dataset was taken from the StatLib library which is maintained at Carnegie Mellon University.\\n\\nThe Boston house-price data of Harrison, D. and Rubinfeld, D.L. 'Hedonic\\nprices and the demand for clean air', J. Environ. Economics & Management,\\nvol.5, 81-102, 1978.   Used in Belsley, Kuh & Welsch, 'Regression diagnostics\\n...', Wiley, 1980.   N.B. Various transformations are used in the table on\\npages 244-261 of the latter.\\n\\nThe Boston house-price data has been used in many machine learning papers that address regression\\nproblems.   \\n     \\n.. topic:: References\\n\\n   - Belsley, Kuh & Welsch, 'Regression diagnostics: Identifying Influential Data and Sources of Collinearity', Wiley, 1980. 244-261.\\n   - Quinlan,R. (1993). Combining Instance-Based and Model-Based Learning. In Proceedings on the Tenth International Conference of Machine Learning, 236-243, University of Massachusetts, Amherst. Morgan Kaufmann.\\n\",\n",
       " 'filename': 'boston_house_prices.csv',\n",
       " 'data_module': 'sklearn.datasets.data'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = load_boston()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "517d35c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = data.data\n",
    "y = data.target\n",
    "feature_names = data.feature_names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "461e4f6d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CRIM</th>\n",
       "      <th>ZN</th>\n",
       "      <th>INDUS</th>\n",
       "      <th>CHAS</th>\n",
       "      <th>NOX</th>\n",
       "      <th>RM</th>\n",
       "      <th>AGE</th>\n",
       "      <th>DIS</th>\n",
       "      <th>RAD</th>\n",
       "      <th>TAX</th>\n",
       "      <th>PTRATIO</th>\n",
       "      <th>B</th>\n",
       "      <th>LSTAT</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.00632</td>\n",
       "      <td>18.0</td>\n",
       "      <td>2.31</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.538</td>\n",
       "      <td>6.575</td>\n",
       "      <td>65.2</td>\n",
       "      <td>4.0900</td>\n",
       "      <td>1.0</td>\n",
       "      <td>296.0</td>\n",
       "      <td>15.3</td>\n",
       "      <td>396.90</td>\n",
       "      <td>4.98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.02731</td>\n",
       "      <td>0.0</td>\n",
       "      <td>7.07</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.469</td>\n",
       "      <td>6.421</td>\n",
       "      <td>78.9</td>\n",
       "      <td>4.9671</td>\n",
       "      <td>2.0</td>\n",
       "      <td>242.0</td>\n",
       "      <td>17.8</td>\n",
       "      <td>396.90</td>\n",
       "      <td>9.14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.02729</td>\n",
       "      <td>0.0</td>\n",
       "      <td>7.07</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.469</td>\n",
       "      <td>7.185</td>\n",
       "      <td>61.1</td>\n",
       "      <td>4.9671</td>\n",
       "      <td>2.0</td>\n",
       "      <td>242.0</td>\n",
       "      <td>17.8</td>\n",
       "      <td>392.83</td>\n",
       "      <td>4.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.03237</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.18</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.458</td>\n",
       "      <td>6.998</td>\n",
       "      <td>45.8</td>\n",
       "      <td>6.0622</td>\n",
       "      <td>3.0</td>\n",
       "      <td>222.0</td>\n",
       "      <td>18.7</td>\n",
       "      <td>394.63</td>\n",
       "      <td>2.94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.06905</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.18</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.458</td>\n",
       "      <td>7.147</td>\n",
       "      <td>54.2</td>\n",
       "      <td>6.0622</td>\n",
       "      <td>3.0</td>\n",
       "      <td>222.0</td>\n",
       "      <td>18.7</td>\n",
       "      <td>396.90</td>\n",
       "      <td>5.33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>501</th>\n",
       "      <td>0.06263</td>\n",
       "      <td>0.0</td>\n",
       "      <td>11.93</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.573</td>\n",
       "      <td>6.593</td>\n",
       "      <td>69.1</td>\n",
       "      <td>2.4786</td>\n",
       "      <td>1.0</td>\n",
       "      <td>273.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>391.99</td>\n",
       "      <td>9.67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>502</th>\n",
       "      <td>0.04527</td>\n",
       "      <td>0.0</td>\n",
       "      <td>11.93</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.573</td>\n",
       "      <td>6.120</td>\n",
       "      <td>76.7</td>\n",
       "      <td>2.2875</td>\n",
       "      <td>1.0</td>\n",
       "      <td>273.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>396.90</td>\n",
       "      <td>9.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>503</th>\n",
       "      <td>0.06076</td>\n",
       "      <td>0.0</td>\n",
       "      <td>11.93</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.573</td>\n",
       "      <td>6.976</td>\n",
       "      <td>91.0</td>\n",
       "      <td>2.1675</td>\n",
       "      <td>1.0</td>\n",
       "      <td>273.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>396.90</td>\n",
       "      <td>5.64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>504</th>\n",
       "      <td>0.10959</td>\n",
       "      <td>0.0</td>\n",
       "      <td>11.93</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.573</td>\n",
       "      <td>6.794</td>\n",
       "      <td>89.3</td>\n",
       "      <td>2.3889</td>\n",
       "      <td>1.0</td>\n",
       "      <td>273.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>393.45</td>\n",
       "      <td>6.48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>505</th>\n",
       "      <td>0.04741</td>\n",
       "      <td>0.0</td>\n",
       "      <td>11.93</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.573</td>\n",
       "      <td>6.030</td>\n",
       "      <td>80.8</td>\n",
       "      <td>2.5050</td>\n",
       "      <td>1.0</td>\n",
       "      <td>273.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>396.90</td>\n",
       "      <td>7.88</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>506 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        CRIM    ZN  INDUS  CHAS    NOX     RM   AGE     DIS  RAD    TAX  \\\n",
       "0    0.00632  18.0   2.31   0.0  0.538  6.575  65.2  4.0900  1.0  296.0   \n",
       "1    0.02731   0.0   7.07   0.0  0.469  6.421  78.9  4.9671  2.0  242.0   \n",
       "2    0.02729   0.0   7.07   0.0  0.469  7.185  61.1  4.9671  2.0  242.0   \n",
       "3    0.03237   0.0   2.18   0.0  0.458  6.998  45.8  6.0622  3.0  222.0   \n",
       "4    0.06905   0.0   2.18   0.0  0.458  7.147  54.2  6.0622  3.0  222.0   \n",
       "..       ...   ...    ...   ...    ...    ...   ...     ...  ...    ...   \n",
       "501  0.06263   0.0  11.93   0.0  0.573  6.593  69.1  2.4786  1.0  273.0   \n",
       "502  0.04527   0.0  11.93   0.0  0.573  6.120  76.7  2.2875  1.0  273.0   \n",
       "503  0.06076   0.0  11.93   0.0  0.573  6.976  91.0  2.1675  1.0  273.0   \n",
       "504  0.10959   0.0  11.93   0.0  0.573  6.794  89.3  2.3889  1.0  273.0   \n",
       "505  0.04741   0.0  11.93   0.0  0.573  6.030  80.8  2.5050  1.0  273.0   \n",
       "\n",
       "     PTRATIO       B  LSTAT  \n",
       "0       15.3  396.90   4.98  \n",
       "1       17.8  396.90   9.14  \n",
       "2       17.8  392.83   4.03  \n",
       "3       18.7  394.63   2.94  \n",
       "4       18.7  396.90   5.33  \n",
       "..       ...     ...    ...  \n",
       "501     21.0  391.99   9.67  \n",
       "502     21.0  396.90   9.08  \n",
       "503     21.0  396.90   5.64  \n",
       "504     21.0  393.45   6.48  \n",
       "505     21.0  396.90   7.88  \n",
       "\n",
       "[506 rows x 13 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(x,columns = feature_names)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac016f7c",
   "metadata": {},
   "source": [
    "## 线性回归模型建立"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "561fce4c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7279250285365975\n"
     ]
    }
   ],
   "source": [
    "# 划分数据集\n",
    "Xtrain, Xtest, Ytrain, Ytest = train_test_split(df,y,test_size=0.3,random_state=420)\n",
    "\n",
    "lr = LinearRegression()\n",
    "lr.fit(Xtrain,Ytrain)\n",
    "lr.predict(Xtrain)\n",
    "print(lr.score(Xtrain,Ytrain))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fbce288e",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 模型评估"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91e6dfde",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### MSE、MAE 预测误差计算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "228856e6",
   "metadata": {
    "hidden": true
   },
   "source": [
    "- 衡量 预测标签和真实标签 之间的均方误差关系\n",
    "- MSE、MAE都是趋于0效果越好 （二者选一个就行）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0644dd3b",
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "from sklearn.metrics import mean_squared_error     #MSE 均方误差\n",
    "from sklearn.metrics import mean_absolute_error    #MAE 绝对均值误差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5d18e4d2",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21.958821745285388"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 评估训练集集合情况  参数1:真实标签 参数2:预测标签\n",
    "mean_squared_error(Ytrain,lr.predict(Xtrain))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "77bf66a3",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.2246903928659543"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 评估训练集集合情况  参数1:真实标签 参数2:预测标签\n",
    "mean_absolute_error(Ytrain,lr.predict(Xtrain))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "563b8d6d",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 交叉验证"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc973d49",
   "metadata": {
    "hidden": true
   },
   "source": [
    "- 可是以多类型的参数，MSE取负值，可应用R2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "0cf69115",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-22.48562665, -13.31846073, -62.53016893, -14.95062335,\n",
       "       -32.26943753, -23.9510441 , -19.81397051, -12.38050342,\n",
       "       -40.2488459 , -11.0330919 ])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cross_val_score(lr,Xtrain,Ytrain,cv=10,scoring='neg_mean_squared_error')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "578f0368",
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-25.29817730234563"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cross_val_score(lr,Xtrain,Ytrain,cv=10,scoring='neg_mean_squared_error').mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d23a940f",
   "metadata": {},
   "source": [
    "## 查看模型系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f8ec3777",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-5.91360708e-02,  3.59005764e-02, -1.75848489e-02,  3.49991909e+00,\n",
       "       -1.59154518e+01,  3.87516690e+00,  5.92507608e-03, -1.24195876e+00,\n",
       "        3.20724778e-01, -1.38381096e-02, -9.18096657e-01,  1.19200101e-02,\n",
       "       -5.13678354e-01])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr.coef_ # 斜率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b3b65560",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32.97761193224176"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr.intercept_ # 截距"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de233d87",
   "metadata": {},
   "source": [
    "## 将数据标准化后再进行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5ec3ec9",
   "metadata": {},
   "source": [
    "- 可以略微提升模型效果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "8161e8a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e2c58a1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "std = StandardScaler()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "21fa9574",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.4462439 , -0.49344656, -0.87354974, ...,  0.79148974,\n",
       "         0.43029664, -0.29832305],\n",
       "       [-0.44473835,  0.66598581, -0.9235792 , ...,  0.23454947,\n",
       "         0.45163257, -1.03715258],\n",
       "       [ 0.98663253, -0.49344656,  1.08366318, ...,  0.79148974,\n",
       "         0.27604496,  0.74893627],\n",
       "       ...,\n",
       "       [ 0.52726644, -0.49344656,  1.08366318, ...,  0.79148974,\n",
       "         0.42641737,  0.61995655],\n",
       "       [-0.43764461, -0.49344656, -0.02153297, ...,  0.32737285,\n",
       "         0.39048317, -1.00490765],\n",
       "       [ 0.33330423, -0.49344656,  1.08366318, ...,  0.79148974,\n",
       "        -2.57735581,  0.49518268]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train_std = std.fit_transform(Xtrain)\n",
    "X_train_std"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "96e0264f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7279250285365977\n",
      "0.7473672442916706\n"
     ]
    }
   ],
   "source": [
    "lr2 = LinearRegression()\n",
    "lr2 = lr2.fit(X_train_std,Ytrain)\n",
    "print(lr2.score(X_train_std,Ytrain))\n",
    "print(lr2.score(std.fit_transform(Xtest),Ytest))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd426910",
   "metadata": {},
   "source": [
    "# 绘制拟合图像"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "06ad8f78",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYAAAAD5CAYAAAAuneICAAAAOXRFWH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CIuJzpUsngKioKGRlZWHjxo3QarXIzMyUOyQAwMGDB3HixAnU1NRg8+bNAIAZM2YoMlaRks5l//798Yc//AHPP/886urqcM0112D8+PEYPnw43n33XVy6dAlZWVnQaOT7eE+cOBE//fQTXnjhBQDAbbfdhuHDhyvmHIpUKhUWLFjQ4bxpNBpFxHrixAkcOHAAx48fx9dffw0AuPPOO3H77bc7jFsppM6r0nAtICKiENWlxwCIiEgaEwARUYhiAiAiClFMAEREIYoJgIgoRDEBEBGFKCYAIqIQ9f8B8XRGC4i28+EAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(range(len(Ytest)),sorted(Ytest),s=5,c= 'b',label='True')\n",
    "plt.scatter(range(len(Ytest)),lr.predict(Xtest)[np.argsort(Ytest)], s=5, c='r',label='Predict', alpha=0.5)\n",
    "plt.legend()\n",
    "plt.ylabel('预测结果')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60c013e8",
   "metadata": {},
   "source": [
    "# 多项式拟合"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d9c7d9c",
   "metadata": {},
   "source": [
    "先拟合函数，再通过函数转化数据，再通过数据训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "32f23305",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import PolynomialFeatures"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3213f054",
   "metadata": {},
   "outputs": [],
   "source": [
    "poly = PolynomialFeatures(degree=2).fit(df,y)  # 这个时候的df是自变量，正常情况下用 Xtrain\n",
    "\n",
    "X_ = poly.transform(df)  #多项式变化后\n",
    "reg = LinearRegression().fit(X_,y)#使用转化后的数据进行建模训练"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bdbd549",
   "metadata": {},
   "source": [
    "## 查看系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "9a7bce17",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('1', -797162270.4324455),\n",
       " ('CRIM', -4.475603586258522),\n",
       " ('ZN', 0.1863235967853845),\n",
       " ('INDUS', -4.649500945558892),\n",
       " ('CHAS', 29.909622846897168),\n",
       " ('NOX', 134.63579084761795),\n",
       " ('RM', 21.726615340547863),\n",
       " ('AGE', 0.9032467377773601),\n",
       " ('DIS', -7.419235800715527),\n",
       " ('RAD', 1.8165347736816586),\n",
       " ('TAX', 0.061943093248600985),\n",
       " ('PTRATIO', 6.449456855996711),\n",
       " ('B', 0.12994783732555654),\n",
       " ('LSTAT', 0.73474220065728),\n",
       " ('CRIM^2', 0.0019979321578842534),\n",
       " ('CRIM ZN', 0.33474112515462995),\n",
       " ('CRIM INDUS', 0.4025500059639287),\n",
       " ('CRIM CHAS', 2.4820570263880155),\n",
       " ('CRIM NOX', -0.7143212277320451),\n",
       " ('CRIM RM', 0.11280888739983254),\n",
       " ('CRIM AGE', -0.00473501123141527),\n",
       " ('CRIM DIS', -0.07473350134866505),\n",
       " ('CRIM RAD', 0.19280422719497373),\n",
       " ('CRIM TAX', -0.023419926332374066),\n",
       " ('CRIM PTRATIO', 0.3901699899571265),\n",
       " ('CRIM B', -0.00019865711316184587),\n",
       " ('CRIM LSTAT', 0.01923722102103355),\n",
       " ('ZN^2', -2.2241863957703223e-05),\n",
       " ('ZN INDUS', -0.003246880539542918),\n",
       " ('ZN CHAS', -0.020308875090152667),\n",
       " ('ZN NOX', -1.0933312461836664),\n",
       " ('ZN RM', 0.005955743819534592),\n",
       " ('ZN AGE', 0.0007605470279627435),\n",
       " ('ZN DIS', -0.010117227600026979),\n",
       " ('ZN RAD', -0.01248509439926071),\n",
       " ('ZN TAX', 0.0006806045865914712),\n",
       " ('ZN PTRATIO', -0.002256034443256727),\n",
       " ('ZN B', 0.0005526240701101415),\n",
       " ('ZN LSTAT', -0.006385744027768148),\n",
       " ('INDUS^2', 0.05256910143140736),\n",
       " ('INDUS CHAS', 0.031267405675247203),\n",
       " ('INDUS NOX', 1.078361824491973),\n",
       " ('INDUS RM', 0.23166010602673498),\n",
       " ('INDUS AGE', 0.004996385779104884),\n",
       " ('INDUS DIS', 0.1746922459185455),\n",
       " ('INDUS RAD', -0.03284192548097664),\n",
       " ('INDUS TAX', 0.0007664829439044674),\n",
       " ('INDUS PTRATIO', -0.03694847783856492),\n",
       " ('INDUS B', 0.003309999451465373),\n",
       " ('INDUS LSTAT', -0.015296497393805453),\n",
       " ('CHAS^2', 29.90962284883595),\n",
       " ('CHAS NOX', -34.184294749805275),\n",
       " ('CHAS RM', -5.523306792850754),\n",
       " ('CHAS AGE', -0.03254914743573578),\n",
       " ('CHAS DIS', 1.0958660041840973),\n",
       " ('CHAS RAD', -0.07762661275854257),\n",
       " ('CHAS TAX', 0.011039977852775995),\n",
       " ('CHAS PTRATIO', -0.9469197915888373),\n",
       " ('CHAS B', 0.021178043960238896),\n",
       " ('CHAS LSTAT', -0.25040143852098834),\n",
       " ('NOX^2', -35.00250596638988),\n",
       " ('NOX RM', 1.6429240125597684),\n",
       " ('NOX AGE', -0.4233768242601123),\n",
       " ('NOX DIS', 14.422245305680963),\n",
       " ('NOX RAD', -1.9328348224582457),\n",
       " ('NOX TAX', 0.16356516278910926),\n",
       " ('NOX PTRATIO', -11.06286238730878),\n",
       " ('NOX B', -0.0035208227046011897),\n",
       " ('NOX LSTAT', 1.1568804013219496),\n",
       " ('RM^2', 0.38322450065589053),\n",
       " ('RM AGE', -0.06288192166742723),\n",
       " ('RM DIS', 0.04219582560842089),\n",
       " ('RM RAD', -0.04852852068005489),\n",
       " ('RM TAX', -0.01563612929373992),\n",
       " ('RM PTRATIO', -0.5778627259781235),\n",
       " ('RM B', -0.004214973341946759),\n",
       " ('RM LSTAT', -0.17313550439322178),\n",
       " ('AGE^2', 0.00016972733864051293),\n",
       " ('AGE DIS', -0.006892559735513615),\n",
       " ('AGE RAD', 0.01573146044682039),\n",
       " ('AGE TAX', -0.0006315053049377184),\n",
       " ('AGE PTRATIO', 0.007773820472237958),\n",
       " ('AGE B', -0.0008894350592036915),\n",
       " ('AGE LSTAT', -0.008469111894687575),\n",
       " ('DIS^2', 0.43892323448050197),\n",
       " ('DIS RAD', -0.07833854249966007),\n",
       " ('DIS TAX', -0.006398015218067599),\n",
       " ('DIS PTRATIO', -0.1994054584868804),\n",
       " ('DIS B', -0.001146378842650897),\n",
       " ('DIS LSTAT', 0.08233122171943849),\n",
       " ('RAD^2', -0.11889701539530129),\n",
       " ('RAD TAX', 0.006971999754327829),\n",
       " ('RAD PTRATIO', -0.07098215371052777),\n",
       " ('RAD B', 0.0007573840643910756),\n",
       " ('RAD LSTAT', -0.022748795005177236),\n",
       " ('TAX^2', -9.199350809652174e-05),\n",
       " ('TAX PTRATIO', 0.005470740866414303),\n",
       " ('TAX B', -0.00017646790442604754),\n",
       " ('TAX LSTAT', -0.0007051938968380966),\n",
       " ('PTRATIO^2', 0.022327563394965555),\n",
       " ('PTRATIO B', 0.0026651052883698567),\n",
       " ('PTRATIO LSTAT', -0.005383593059399394),\n",
       " ('B^2', -5.88011982847747e-05),\n",
       " ('B LSTAT', -0.00022656063245385383),\n",
       " ('LSTAT^2', 0.015763857133864967)]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[*zip([i for i in poly.get_feature_names_out(df.columns)],reg.coef_)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "052a2df8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>-797162270.432446</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>CRIM</td>\n",
       "      <td>-4.475604</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ZN</td>\n",
       "      <td>0.186324</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>INDUS</td>\n",
       "      <td>-4.649501</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>CHAS</td>\n",
       "      <td>29.909623</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>100</th>\n",
       "      <td>PTRATIO B</td>\n",
       "      <td>0.002665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101</th>\n",
       "      <td>PTRATIO LSTAT</td>\n",
       "      <td>-0.005384</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102</th>\n",
       "      <td>B^2</td>\n",
       "      <td>-0.000059</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>103</th>\n",
       "      <td>B LSTAT</td>\n",
       "      <td>-0.000227</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>104</th>\n",
       "      <td>LSTAT^2</td>\n",
       "      <td>0.015764</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>105 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                 0                 1\n",
       "0                1 -797162270.432446\n",
       "1             CRIM         -4.475604\n",
       "2               ZN          0.186324\n",
       "3            INDUS         -4.649501\n",
       "4             CHAS         29.909623\n",
       "..             ...               ...\n",
       "100      PTRATIO B          0.002665\n",
       "101  PTRATIO LSTAT         -0.005384\n",
       "102            B^2         -0.000059\n",
       "103        B LSTAT         -0.000227\n",
       "104        LSTAT^2          0.015764\n",
       "\n",
       "[105 rows x 2 columns]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "coeff = pd.DataFrame([poly.get_feature_names_out(df.columns),reg.coef_.tolist()]).T\n",
    "coeff"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b9f7853",
   "metadata": {},
   "source": [
    "## 拟合效果对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "7e60dca8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7406426641094095"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 正常模型 得分\n",
    "lr3 = LinearRegression().fit(x,y)\n",
    "lr3.score(x,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b45b3551",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9239966560264268"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 多项式拟合后 得分\n",
    "lr4 = LinearRegression().fit(X_,y)\n",
    "lr4.score(X_,y)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "311.634px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
